package me.jessyan.mvparms.demo.mvp.ui.activity.car;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.xrecyclerview.XRecyclerView;
import com.example.xrecyclerview.refresh.SwipyRefreshLayout;
import com.example.xrecyclerview.refresh.SwipyRefreshLayoutDirection;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.base.DefaultAdapter;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.DoubleClickUtils;
import com.jess.arms.widget.CustomPopupWindow;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.jessyan.mvparms.demo.R;
import me.jessyan.mvparms.demo.di.component.car.DaggerMyCarsComponent;
import me.jessyan.mvparms.demo.di.module.car.MyCarsModule;
import me.jessyan.mvparms.demo.mvp.contract.car.MyCarsContract;
import me.jessyan.mvparms.demo.mvp.presenter.car.MyCarsPresenter;
import me.jessyan.mvparms.demo.mvp.ui.adapter.MyCarsAdapter;
import me.jessyan.mvparms.demo.mvp.ui.fragment.VehicleDialog;
import me.jessyan.mvparms.demo.mvp.ui.widget.CustomToolbar;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * 我的车辆
 */
public class MyCarsActivity extends BaseActivity<MyCarsPresenter> implements MyCarsContract.View {
    @BindView(R.id.recyclerView)
    XRecyclerView recyclerView;
    @BindView(R.id.refresh)
    SwipyRefreshLayout refreshLayout;
    @BindView(R.id.toolbar)
    CustomToolbar toolbar;

    MyCarsAdapter adapter;

    @Inject
    CustomPopupWindow unBindPop;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerMyCarsComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .myCarsModule(new MyCarsModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_my_cars; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        // TODO: 2019/3/8 0008  车辆还有个已经绑满的装态
        
        initRecycler();

        toolbar.setListener(new CustomToolbar.ToolbarListener() {
            @Override
            public void onLeftBack() {
                killMyself();
            }

            @Override
            public void onRightBack() {

            }
        });

        unBindPop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1);
            }
        });
    }

    private void initRecycler() {

        List<String> list = new ArrayList<>();

        adapter = new MyCarsAdapter(list);
        refreshLayout.setOnRefreshListener(new SwipyRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(SwipyRefreshLayoutDirection direction) {
                list.add("s");
                list.add("s");
                list.add("s");
                adapter.notifyDataSetChanged();
            }
        });
        LinearLayoutManager lr = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false);

        recyclerView.setLayoutManager(lr);
        recyclerView.setAdapter(adapter);
        ArmsUtils.configRecyclerView(recyclerView, lr);
        recyclerView.setPullRefreshEnabled(false);
        recyclerView.clearHeader();
        recyclerView.setLoadingMoreEnabled(false);


        adapter.setOnItemClickListener(new DefaultAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int viewType, Object data, int position) {
                if (view instanceof TextView && ((TextView) view).getText().equals("解绑")) {
                    if(unBindPop!=null){
                    unBindPop.show();
                    backgroundAlpha(0.7f);
                    }
                } else {

                }
            }
        });

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }



    @OnClick({  R.id.floatButton})
    public void onViewClicked(View view) {
        if(DoubleClickUtils.getInstance().isDouble(view)){
            return;
        }
        switch (view.getId()) {

            case R.id.floatButton:
                new VehicleDialog().show(getSupportFragmentManager());
                break;
                default:
        }
    }

    @Override
    public MyCarsActivity getActivity() {
        return this;
    }

    @Override
    public void popBackUnBind() {
        showMessage("解绑");
    }

    @Override
    public void popDismiss() {
        if(unBindPop!=null&&unBindPop.isShowing()){
            unBindPop.dismiss();
        }
    }
}
